from datetime import datetime
from json import dumps

from flask import Blueprint, jsonify, Flask, current_app, make_response, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from flask_restx import Resource, Api

from exts import db
from models import order

qa_bp = Blueprint("qa",__name__,url_prefix="/admin")

api = Api(qa_bp)

# @qa_bp.route("/")
# @jwt_required(optional=False)
# class ArticleResource(Resource):
#    def get(self):
#       id =get_jwt_identity()
#       return ""
@api.route('/')
class DemoResource(Resource):
    @jwt_required(optional=False)
    def get(self):
        user =get_jwt_identity()
        return user
@qa_bp.route('/order/list')
# class OrderList(Resource):
#     @jwt_required(optional=False)
def order():
        per_page = int(request.args.get("per_page") or 10)
        page = int(request.args.get("page") or 1)
        user_name = request.args.get("user_name")
        query = Order.query
        if user_name:
            query = query.filter_by(user_name=user_name)
        count = query.count()
        order = query.limit(per_page).offset((page-1) * per_page).all()
        return {"data":order,"code":1,"total":count}
@qa_bp.route('/order/detail/<id>')
# class OrderList(Resource):
#     @jwt_required(optional=False)
def orderDetail(id):
        order = Order.query.filter_by(order_sn=id).first()
        return {"data":order,"code":1}
@qa_bp.route('/order/delete/<id>',methods=["DELETE"])
def orderDelete(id):
    order = Order.query.filter_by(id=id).delete()
    db.session.commit()
    return {"data": order, "code": 1}

